package com.Apo1o.service.Impl;

import com.Apo1o.bean.ManagerFenceHouse;
import com.Apo1o.bean.ManagerHurdles;
import com.Apo1o.dao.ManagerFenceHouseMapper;
import com.Apo1o.exception.CustomerException;
import com.Apo1o.response.ResultCode;
import com.Apo1o.service.IFenceHouseService;
import com.alibaba.druid.sql.visitor.functions.If;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @Auther: Apo1o.
 * @Date: 2023/10/31-10-31-14:50
 * @Description： com.Apo1o.service.Impl
 */
@Service
@RequiredArgsConstructor
public class FenceHouseServiceImpl implements IFenceHouseService {

    private    final ManagerFenceHouseMapper managerFenceHouseMapper;
    @Override
    public PageInfo<ManagerFenceHouse> query(Integer pageNum , Integer pageSize, String fhName) {
        if (pageNum == null || pageSize==null)throw  new CustomerException(ResultCode.PARAM_NULL);

        PageHelper.startPage(pageNum,pageSize);
        List<ManagerFenceHouse> list =managerFenceHouseMapper.queryAll(fhName);
        System.out.println(list);
        PageInfo<ManagerFenceHouse>pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void deleteById(String id) {
        if (id==null)throw new CustomerException(ResultCode.DATA_NOT_EXITS);
        managerFenceHouseMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByBatch(List<String> list) {
        list.forEach(e-> {
            if (managerFenceHouseMapper.selectByPrimaryKey(e) != null){
                managerFenceHouseMapper.deleteByPrimaryKey(e);
            }else {
                throw new CustomerException(ResultCode.DATA_NOT_EXITS);
            }
        });
    }

    @Override
    public void saveOrUpdate(ManagerFenceHouse managerFenceHouse) {
        System.out.println(managerFenceHouse.getFhId());
       if (managerFenceHouse.getFhId() == null || managerFenceHouse.getFhId().isEmpty()){
           // 生成新的UUID
           String newId= UUID.randomUUID()
                   .toString()
                   .replace("-", "");
           // 在插入新记录时，将newId分配给ID字段
           // 这个newId可以作为字符串存储在数据库中

           managerFenceHouse.setFhId(newId);
           managerFenceHouseMapper.insert(managerFenceHouse);}
       else {
           managerFenceHouseMapper.updateByPrimaryKeySelective(managerFenceHouse);
       }
    }

    @Override
    public List<ManagerFenceHouse> rnm() {
        return managerFenceHouseMapper.rnm();
    }

    @Override
    public List<ManagerHurdles> selectById(String hId) {
        return managerFenceHouseMapper.selectById(hId);
    }


}
